﻿using DataAccess;
using Model;
using SH3H.CSM.Common.LogUtil;
using SH3H.SDK.Infrastructure.Caching;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;

namespace Business
{
    public class OperateBusiness
    {

        public int DealTask(int i)
        {
            //LogUtil.Info("Task-Porcess-" + i.ToString());
            DateTime now = DateTime.Now;
            DateTime time = DateTime.Now;
            while (time.AddSeconds(1) > now)
            {
                now = DateTime.Now;
            }
            return i;
        }

        public int DealTask(int i, CountdownEvent c, ref List<int> l)
        {
            
            //LogUtil.Info("Task2-Porcess-" + i.ToString());
            DateTime now = DateTime.Now;
            DateTime time = DateTime.Now;
            while (time.AddSeconds(1) > now)
            {
                now = DateTime.Now;
            }
            c.Signal();
            lock (l)
            {
                l.Add(i);
            }
            return i;
        }

        public int DealTaskWithDataAccess(int i, CountdownEvent c, ref List<int> l)
        {

            //LogUtil.Info("Task2-Porcess-" + i.ToString());
            OperateDataAccess access = new OperateDataAccess();
            int data = access.GetData(i);
            c.Signal();
            lock (l)
            {
                l.Add(data);
            }
            return data;
        }

        public int TestTran(int id,CountdownEvent c=null) {
            int res = -111;
            using (TransactionScope sc = new TransactionScope())
            {
                try
                {
                    OperateDataAccess access = new OperateDataAccess();
                    bool update = access.UpdateTimes(id);
                    if (!update)
                    {
                        sc.Dispose();
                    }
                    int data = access.GetData(id);
                    if (data == -1)
                    {
                        sc.Dispose();
                    }
                    res = data;
                    sc.Complete();
                }
                catch (Exception e) {
                    sc.Dispose();
                    LogUtil.Error(e.Message, e);
                }

            }
            if (c != null) {
                c.Signal();
            }
            return res;
        }

        /// <summary>
        /// 测试批量插入
        /// </summary>
        /// <param name="id"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public int TestTrans(int id, CountdownEvent c = null) {
            int res = -111;
            using (TransactionScope sc = new TransactionScope())
            {
                try
                {
                    OperateDataAccess access = new OperateDataAccess();
                    bool insert = access.sqlBulkCopy();
                    if (!insert)
                    {
                        res = -100;
                        sc.Dispose();
                        return res;
                    }
                    int update = -1;//access.Pro(id);
                    if (update!=0)
                    {
                        res =update;
                        sc.Dispose();
                        return res;
                    }
                    res = 0;
                    sc.Complete();
                }
                catch (Exception e)
                {
                    sc.Dispose();
                    LogUtil.Error(e.Message, e);
                }

            }
            if (c != null)
            {
                c.Signal();
            }
            return res;
        }

        public bool AddCache() {
            List<CacheModel> m = new List<CacheModel>();
            for(int i=1;i<5;i++){
                m.Add(new CacheModel
                {
                    id = i,
                    value = i
                });
            }
            var arr=m.OrderBy(s=>s.id);
            try {
                CacheManager.Get().Add<List<CacheModel>>("TC", m);
                return true;
            }
            catch(Exception e){
                return false;
            }
        }

        public IEnumerable<CacheModel> GetCache()
        {
            var res = CacheManager.Get().Get<List<CacheModel>>("TC");
            return res;
        }

        public string GetMySqlData() {
            OperateDataAccess access = new OperateDataAccess();
            return access.getData();

        }

        public bool TestTranNew() {
            using (TransactionScope sc = new TransactionScope())
            {
                OperateDataAccess access = new OperateDataAccess();
                try
                {
                    access.sqlBulkCopy();
                    bool update = access.UpdateTimes(1);
                    if (update)
                    {
                        sc.Complete();
                        return true;
                    }
                    else {
                        sc.Complete();
                        return false;
                    }
                }
                catch(Exception ex){
                    sc.Dispose();
                    LogUtil.Error(ex.Message, ex);
                    return false;
                }
            }
        }
    }
}
